<?php

namespace Handler;

use Config\Cfg;

class TPHandler {
	/**
	 * 单例
	 *
	 * @var TPHandler
	 */
	private static $m_Instance;
	/**
	 *
	 * @var string
	 */
	private $m_Url = Cfg::TP_URL;
	/**
	 * 构造函数
	 */
	private function __construct() {
	}
	/**
	 * 复制方法
	 */
	private function __clone() {
	}
	/**
	 * 获取单例
	 *
	 * @return TPHandler
	 */
	public static function mb_GetInstance() {
		if (! (self::$m_Instance instanceof self)) {
			self::$m_Instance = new self ();
		}
		return self::$m_Instance;
	}
	public function mb_CheckWeChatAccountToTp($openId) {
		$url = $this->m_Url . 'Lobby/mb_CheckWeChatAccount';
		$data = new \stdClass ();
		$data->openid = $openId;
		return $this->mb_DecodeTPData ( $this->mb_SendPost ( $url, array (
				'data' => $this->mb_PackData ( $data ) 
		) ) );
	}
	public function mb_WeChatRegistToTP($nick, $head, $sex, $openId, $unionId, $code) {
		$url = $this->m_Url . 'Lobby/mb_WeChatRegist';
		$data = new \stdClass ();
		$data->nick = $nick;
		$data->head = $head;
		$data->sex = $sex;
		$data->openid = $openId;
		$data->unionid = $unionId;
		$data->code = $code;
		return $this->mb_DecodeTPData ( $this->mb_SendPost ( $url, array (
				'data' => $this->mb_PackData ( $data ) 
		) ) );
	}
	
	public function mb_WeChatLoginToTP($nick, $head, $sex, $openId, $unionId) {
		$url = $this->m_Url . 'Lobby/mb_WeChatLogin';
		$data = new \stdClass ();
		$data->nick = $nick;
		$data->head = $head;
		$data->sex = $sex;
		$data->openid = $openId;
		$data->unionid = $unionId;
		return $this->mb_DecodeTPData ( $this->mb_SendPost ( $url, array (
				'data' => $this->mb_PackData ( $data )
		) ) );
	}
	/**
	 * 请求注册
	 *
	 * @param unknown $name        	
	 * @param unknown $nick        	
	 * @param unknown $pass        	
	 * @return \stdClass
	 */
	public function mb_RegistToTP($name, $nick, $pass, $agent, $phone) {
		$url = $this->m_Url . 'Lobby/mb_AccountRegist';
		$data = new \stdClass ();
		$data->name = $name;
		$data->nick = $nick;
		$data->pass = $pass;
		$data->code = $agent;
		$data->phone = $phone;
		return $this->mb_DecodeTPData ( $this->mb_SendPost ( $url, array (
				'data' => $this->mb_PackData ( $data ) 
		) ) );
	}
	/**
	 * 请求登录
	 *
	 * @param unknown $name        	
	 * @param unknown $pass        	
	 * @return stdClass
	 */
	public function mb_LoginToTP($name, $pass) {
		$url = $this->m_Url . 'Lobby/mb_AccountLogin';
		$data = new \stdClass ();
		$data->name = $name;
		$data->pass = $pass;
		return $this->mb_DecodeTPData ( $this->mb_SendPost ( $url, array (
				'data' => $this->mb_PackData ( $data ) 
		) ) );
	}
	/**
	 * 查询数据
	 *
	 * @param unknown $name        	
	 * @return stdClass
	 */
	public function mb_QueryDataToTP($name) {
		$url = $this->m_Url . 'Lobby/mb_AccountQuery';
		$data = new \stdClass ();
		$data->name = $name;
		return $this->mb_DecodeTPData ( $this->mb_SendPost ( $url, array (
				'data' => $this->mb_PackData ( $data ) 
		) ) );
	}
	/**
	 * 保存金币
	 *
	 * @param unknown $name        	
	 * @param unknown $coin        	
	 * @return stdClass
	 */
	public function mb_SaveDataToTP($name, $coin, $gameId, $remark) {
		$url = $this->m_Url . 'Lobby/mb_AccountSave';
		$data = new \stdClass ();
		$data->name = $name;
		$data->coin = $coin;
		$data->gameid = $gameId;
		$data->remark = $remark;
		return $this->mb_DecodeTPData ( $this->mb_SendPost ( $url, array (
				'data' => $this->mb_PackData ( $data ) 
		) ) );
	}
	/**
	 * 重设密码
	 *
	 * @param unknown $name        	
	 * @param unknown $pass        	
	 */
	public function mb_ResetPasswordToTP($name, $pass) {
		$url = $this->m_Url . 'Lobby/mb_AccountResetPass';
		$data = new \stdClass ();
		$data->name = $name;
		$data->pass = $pass;
		return $this->mb_DecodeTPData ( $this->mb_SendPost ( $url, array (
				'data' => $this->mb_PackData ( $data ) 
		) ) );
	}
	/**
	 * 获取大厅信息
	 */
	public function mb_GetLobbyInfoToTP() {
		$url = $this->m_Url . 'Game/mb_GetLobbyInfo';
		$data = new \stdClass ();
		return $this->mb_DecodeTPData ( $this->mb_SendPost ( $url, array (
				'data' => $this->mb_PackData ( $data ) 
		) ) );
	}
	public function mb_GetGameInfoToTP() {
		$url = $this->m_Url . 'Lobby/mb_GetGameInfo';
		$data = new \stdClass ();
		return $this->mb_DecodeTPData ( $this->mb_SendPost ( $url, array (
				'data' => $this->mb_PackData ( $data ) 
		) ) );
	}
	/**
	 *
	 * @return stdClass
	 */
	public function mb_GetGameConfigToTP($gameId) {
		$url = $this->m_Url . 'Lobby/mb_GetGameConfig';
		$data = new \stdClass ();
		$data->gameid = $gameId;
		return $this->mb_DecodeTPData ( $this->mb_SendPost ( $url, array (
				'data' => $this->mb_PackData ( $data ) 
		) ) );
	}
	/**
	 * 解析TP数据
	 *
	 * @param string $reqData        	
	 * @return \stdClass
	 */
	private function mb_DecodeTPData($reqData) {
		$ret = new \stdClass ();
		if ($reqData == null || $reqData == '') {
			$ret->state = false;
			$ret->msg = '外部服务器异常，请稍后再试！';
			return $ret;
		}
		$reqJson = json_decode ( $reqData );
		if (json_last_error () != JSON_ERROR_NONE) {
			$ret->state = false;
			$ret->msg = '外部服务器异常，请稍后再试！';
			return $ret;
		}
		
		$result = $reqJson->m_Result;
		$data = $reqJson->m_Data;
		$sign = $reqJson->m_Sign;
		
		if (md5 ( $result . $data . Cfg::MSG_MD5 ) == $sign) {
			if ($result == ErrCode_Normal) {
				$ret->state = true;
				$ret->data = $data;
			} else {
				$ret->state = false;
				$ret->msg = $data;
			}
			return $ret;
		} else {
			$ret->state = false;
			$ret->msg = '外部服务器异常，请稍后再试！';
			return $ret;
		}
	}
	/**
	 * 打包数据
	 *
	 * @param unknown $data        	
	 * @return string[]|NULL[]
	 */
	private function mb_PackData($data) {
		$pData = array ();
		$pData ['m_Data'] = json_encode ( $data );
		$pData ['m_Sign'] = md5 ( $pData ['m_Data'] . Cfg::MSG_MD5 );
		return json_encode ( $pData );
	}
	/**
	 * 发送POST数据
	 *
	 * @param unknown $url        	
	 * @param unknown $pData        	
	 * @return string
	 */
	private function mb_SendPost($url, $pData) {
		$postdata = http_build_query ( $pData );
		echo "\n";
		echo $url . '?' . $postdata;
		echo "\n";
		$options = array (
				'http' => array (
						'method' => 'POST',
						'header' => 'Content-type:application/x-www-form-urlencoded',
						'content' => $postdata,
						'timeout' => 15 * 60  // 超时时间（单位:s）
				) 
		);
		$context = stream_context_create ( $options );
		$result = file_get_contents ( $url, false, $context );
		return $result;
	}
	/**
	 * 发送GET数据
	 *
	 * @param unknown $url        	
	 * @param unknown $gData        	
	 * @return string
	 */
	private function mb_SendGet($url, $gData) {
		$query = http_build_query ( $gData );
		$result = file_get_contents ( $url . '?' . $query );
		return $result;
	}
}